package com.lkei.controller;

import com.lkei.dto.Result;
import com.lkei.entity.Topic;
import com.lkei.service.TopicService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

//@RestController
//@RequestMapping("topic")
@Controller
public class TopicController {
    @Autowired
    private TopicService topicService;

    @CrossOrigin
    @RequestMapping("findAllTopic")
    public Result findAllTopic(){
        List<Topic> topicList=topicService.findAllTopic();
        return Result.SUCCESS.setNewData(topicList);
    }

    @GetMapping("/topicpage")
    public String topicPage(Model model){
        List<Topic> page = topicService.findAllTopic();
        //List<Tag> tags = tagService.list();
        //model.addAttribute("tag",tags);
        model.addAttribute("page",page);
        //分页查询数据
        //Page<Topic> storePage = new Page<>(pn, 10L);
        //分页查询结果
        //排序
        //QueryWrapper<Topic> queryWrapperx = new QueryWrapper<Topic>();
        //Page<Topic> page = topicService.page(storePage, queryWrapperx.orderByDesc("posttime"));
        //model.addAttribute("page",page);
        //List<Topic> records = page.getRecords();
        model.addAttribute("msg1","删除(仅限本人操作)");
        return "topicpage";
    }

    @PostMapping(value = {"/addnewtopic"})
    public String addTopicPage(Topic topic, HttpSession session, Model model){
//        Timestamp t = new Timestamp(new Date().getTime());
//        SimpleDateFormat sdf = new SimpleDateFormat("yyy");
//        topic.setPosttime(t);
//        topicMapper.insert(topic);
        DateTime time = new DateTime();
        String ti = time.toString("yyyy-MM-dd HH:mm:ss");
        String ti2 = time.toString("yyyyMMddHHmmss");
        topic.setPosttime(ti);
        ti2 = ti2+"124001";
        topic.setTno(ti2);
        topic.setPoster("124001");
        topicService.addTopic(topic);

        return "redirect:/topicpage";
    }


}